<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use think\Db;
class Message extends Common
{
	function messageList(){
		$list = array();
                $p = empty($_GET['p']) ? 1 : $_GET['p'];
                $size = empty($_REQUEST['size']) ? 25 : $_REQUEST['size'];

                $count = db('message')->where($where)->count();// 查询满足要求的总记录数

                $list = db('message')->where($where)->order('add_time desc')->paginate($size,$count)->each(function($item,$key){
                	return $item;
                });
                $page = $list->render();

                $this->assign('total',$count);
                $this->assign('page',$page);
                $this->assign('list',$list);
                return view('Message/messageList');
	}

    function msgRead(){
            $id = input('route.id');
            $msg = db('Message')->find($id);
            $this->assign('msg',$msg);
            return view('Message/msgRead');
    }

    function msgisRead(){
            $is_read['is_read'] = 1;
            $id = input('post.id');
            $msg = db('message')->find($id);
            if($msg){
                    $re = db('message')->where(array('id'=>$id))->update($is_read);
                    if($re !== false){
                            $json = array('status'=>1,'message'=>'留言已标记为已读！');
                            /*********************记录日志***********************/
                            addLog(session('user_name').'留言标记为已读-[ ID:'.$id.' ]');
                    }else{
                            $json = array('status'=>0,'message'=>'操作失败！');
                    }
            }else{
                    $json = array('status'=>0,'message'=>'找不到该留言！');
            }
            echo json_encode($json);
    }

    function msgDel(){
            $id = input('post.id');
            $msg = db('message')->find($id);
            if($msg){
                    $re = db('message')->delete($id);
                    if($re){
                            $json = array('status'=>1,'message'=>'留言删除成功！');
                            /*********************记录日志***********************/
                            addLog(session('user_name').'留言删除成功-[ ID:'.$id.' ]');
                    }else{
                            $json = array('status'=>0,'message'=>'留言删除失败！');
                    }
            }else{
                    $json = array('status'=>0,'message'=>'找不到该留言！');
            }
            echo json_encode($json);return;
    }

    //删除选中
    function delselect(){
        $getData = input('get.');
        $ids = $getData['ids'];
        if(is_array($ids) && !empty($ids)){
            Db::name('message')->where('id','in',implode(',',$ids))->delete();
            $json = ['status'=>1,'message'=>'操作完成','icon'=>1];
            /*********************记录日志***********************/
            addLog(session('user_name').'批量删除留言成功-[ ID:'.implode(',',$ids).' ]');
            echo json_encode($json);exit;
        }else{
            $json = ['status'=>0,'message'=>'请选择选项','icon'=>2];
            echo json_encode($json);exit;
        }
    }

    function reply(){
        $postData = input('post.');
        $id = $postData['id'];
        $data['reply'] = $postData['reply'];
        $data['admin_id'] = session('admin_id');
        $data['is_read'] = 1;
        $data['reply_time'] = time();
        $res = Db::name('message')->where('id',$id)->update($data);
        if($res !== false){
            exitjson(1,'保存成功');
        }else{
            exitjson(0,'保存失败');
        }
    }





}